﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data.MySqlClient;
using System.Data;

namespace GZ.Entity
{
    public class ECategory
    {
        public ECategory()
        {
            this.categoryname = string.Empty;
            this.isdelete = 0;
        }
        #region 属性

        private int id;

        public int Id
        {
            get { return id; }
            set { id = value; }
        }
        private string categoryname;

        public string CategoryName
        {
            get { return categoryname; }
            set { categoryname = value; }
        }
        private int isdelete;

        public int IsDelete
        {
            get { return isdelete; }
            set { isdelete = value; }
        }
        #endregion

        /// <summary>
        /// 得到所有分类
        /// </summary>
        /// <returns>所有未删除分类的列表</returns>
        public DataTable GetCategory()
        {
            string sql = "SELECT * FROM tb_category WHERE IsDelete=0;";
            return MySqlHelper.ExecuteDataset(GZ.DAL.MySQLHelper.connStr, sql).Tables[0];
        }

        /// <summary>
        /// 插入新分类
        /// </summary>
        /// <param name="obj">分类对象</param>
        /// <returns>成功返回True,失败返回False</returns>
        public bool Insert(ECategory obj)
        {
            string sql = @"INSERT INTO tb_category
                            (CategoryName,
                            IsDelete)
                            VALUES
                            (@CategoryName,
                            @IsDelete);";
            MySqlParameter[] parameters = new MySqlParameter[2];
            parameters[0] = new MySqlParameter("@CategoryName", obj.CategoryName);
            parameters[1] = new MySqlParameter("@IsDelete", obj.IsDelete);
            int val = MySqlHelper.ExecuteNonQuery(GZ.DAL.MySQLHelper.connStr, sql, parameters);
            if (val > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 根据Id删除分类
        /// </summary>
        /// <param name="Id">根据分类ID</param>
        /// <returns>成功返回True,失败返回False</returns>
        public bool Del(int Id)
        {
            string sql = "UPDATE tb_category SET IsDelete = 1 WHERE Id=@Id";
            int val = MySqlHelper.ExecuteNonQuery(GZ.DAL.MySQLHelper.connStr, sql, new MySqlParameter[] { new MySqlParameter("@Id", Id) });
            if (val > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}
